package cn.kilo.trigger.job;

import cn.kilo.domain.model.order.entity.OrderStaffEntity;
import cn.kilo.domain.model.order.entity.StaffEntity;
import cn.kilo.domain.model.order.entity.StaffSalaryEntity;
import cn.kilo.domain.repository.IStrategyRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author kilok
 * 定时清理员工
 */
@Slf4j
@Component()
public class UpdateStaff {

    @Resource
    private IStrategyRepository repository;

    @Scheduled(cron = "0 0 21 * *  ?")
    public void test(){
        log.info("清理员工");
        repository.deleteStaff();
        repository.UpdateStaff();
    }

    /**
     * 结算骑手收入
     */
    @Scheduled(cron = "0 0 20 * * ?")
    public void doJob(){
        Map<StaffEntity, List<OrderStaffEntity>> staffEntityListMap = repository.updateSalary();
        Set<StaffEntity> staffEntities = staffEntityListMap.keySet();
        List<StaffSalaryEntity> salaryEntities=new ArrayList<>();
        for(StaffEntity staffEntity:staffEntities){
            // TODO: 2024/5/28 使用方法计算员工薪水
            StaffSalaryEntity salary = OrderStaffEntity.getSalary(staffEntity, staffEntityListMap.get(staffEntity), staffEntity.getRole());
            salaryEntities.add(salary);
        }
        repository.updateDailySalary(salaryEntities);

    }
}
